package com.yuexunit.operationman.nbsy.module.plant.ui;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;


import com.yuexunit.operationman.nbsy.R;
import com.yuexunit.operationman.nbsy.config.AppConfig;
import com.yuexunit.operationman.nbsy.module.plant.bean.ImplantMaterialItemListEntity;
import com.yuexunit.operationman.nbsy.module.plant.bean.MaterialDeviceBean;
import com.yuexunit.operationman.nbsy.module.plant.model.impl.MaterialDeviceModel;
import com.yuexunit.operationman.nbsy.module.plant.presenter.impl.MaterialDevicePresenter;
import com.yuexunit.operationman.nbsy.module.plant.ui.view.IMaterialDeviceView;
import com.yuexunit.operationman.nbsy.widget.LoadingDialog;

import org.kymjs.kjframe.KJActivity;
import org.kymjs.kjframe.ui.ViewInject;

import java.util.HashMap;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by chenss on 2017/7/11 0011.
 */

public class Act_Scan_code extends KJActivity implements IMaterialDeviceView {
    @Bind(R.id.usr_name)
    TextView usrName;
    @Bind(R.id.usr_num)
    TextView usrNum;
    @Bind(R.id.usr_ops)
    TextView usrOps;
    @Bind(R.id.serial_number)
    EditText serialNumber;
    @Bind(R.id.batch_number)
    EditText batchNumber;
    @Bind(R.id.statu_txt)
    TextView statuTxt;
    @Bind(R.id.iv_batch_number)
    LinearLayout ivBatchNumber;

    MaterialDevicePresenter presenter;
    LoadingDialog loadingDialog;
    boolean isSign;

    @Override
    public void setRootView() {
        setContentView(R.layout.act_scan_code);
        ButterKnife.bind(this);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @Override
    public void initData() {
        super.initData();
        setTitle();
        loadingDialog = new LoadingDialog(this);
        presenter = new MaterialDevicePresenter(this, new MaterialDeviceModel());
    }

    private void setTitle()
    {
        if(AppConfig.MATERIALBEAN.getObject().getImplantMaterial().getPatientSex().equals("1")) {
            usrName.setText(AppConfig.MATERIALBEAN.getObject().getImplantMaterial().getPatientName() + "    男");
        }else if(AppConfig.MATERIALBEAN.getObject().getImplantMaterial().getPatientSex().equals("2"))
        {
            usrName.setText(AppConfig.MATERIALBEAN.getObject().getImplantMaterial().getPatientName() + "    女");
        }
        else {
            usrName.setText(AppConfig.MATERIALBEAN.getObject().getImplantMaterial().getPatientName() + "    不详");
        }
        usrNum.setText("住院号" + AppConfig.MATERIALBEAN.getObject().getImplantMaterial().getInHospitalNo());
        usrOps.setText(AppConfig.MATERIALBEAN.getObject().getImplantMaterial().getOperationName());
    }

    @OnClick({R.id.back, R.id.sure, R.id.statu_txt})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.sure:
                if(serialNumber.getText().toString().isEmpty()){
                    ViewInject.toast("编号不能为空");
                    break;
                }
                if(!isSign && batchNumber.getText().toString().isEmpty()){
                    ViewInject.toast("批号不能为空");
                    break;
                }
                Map<String, String> params = new HashMap<>();
                params.put("number", serialNumber.getText().toString());
                presenter.getMaterialDevice(params);
                break;
            case R.id.statu_txt:
                isSign = !isSign;
                if(isSign){
                    statuTxt.setText("双条码");
                    ivBatchNumber.setVisibility(View.GONE);
                }else{
                    statuTxt.setText("单条码");
                    ivBatchNumber.setVisibility(View.VISIBLE);
                }
                break;
        }
    }

    @Override
    public void getMaterialDevice(MaterialDeviceBean<ImplantMaterialItemListEntity> materialDeviceBean) {
        if(materialDeviceBean.getCode() == 200){
            ImplantMaterialItemListEntity materialItem = materialDeviceBean.getObject();
            if(materialItem.getAmount() == null){
                materialItem.setAmount("1");
            }
            materialItem.setBatchNumber(batchNumber.getText().toString());
            Bundle bundle = new Bundle();
            Intent intent = new Intent();
            bundle.putSerializable("materialItem", materialItem);
            intent.putExtras(bundle);
            setResult(10000, intent);
            finish();
        }
    }

    @Override
    public void loadingDialogStatus(int status) {
        switch (status) {
            case AppConfig.SHOW_DIALOG:
                loadingDialog.setCanceledOnTouchOutside(false);
                loadingDialog.show();
                break;
            case AppConfig.DISMISS_DIALOG:
                loadingDialog.dismiss();
                break;

        }
    }
}
